// pages/classManage/classManage.js
let app = getApp()
let env = app.globalData.env
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {}

  },

  createClass() {
    // 创建班级功能
    wx.navigateTo({
      url: '/pages/classCreate/classCreate',
    })
  },
  joinClass() {
    // 加入班级功能
    wx.navigateTo({
      url: '/pages/classJoin/classJoin',
    })
  },
  exitClass(e) {
    let _this = this
    let classData = e.currentTarget.dataset.class;
    let user = this.data.user
    if (classData.createrId == user.userId) {
      wx.showToast({
        title: '教师不能退出班级,删除请左滑',
        icon: "none"
      })
    } else {

      wx.request({
        url: 'https://www.wantstong.com/api/exitClass',
        method: "POST",
        data: {
          classData: classData,
          user: user
        },
        success: res => {
          console.log(res)
          if (res.data.msg == "success") {
            wx.showToast({
              title: '成功',
            })

            wx.request({
              // url: 'http://localhost:88/api/searchClass',
              url: env != 1 ? 'http://localhost:88/api/searchClass' : 'https://www.wantstong.com/api/searchClass',
              method: 'POST',
              data: {
                openId: _this.data.user.userId,
              },
              success: res => {
                console.log(res)
                _this.setData({
                  classData: res.data.res
                })
              }
            })

          }

        }
      })

    }

  },
  gotoClassRoom(item) {

    let classData = item.currentTarget.dataset.class

    wx.setStorage({
      data: classData,
      key: 'clickCourseId',
    })
    wx.navigateTo({
      url: '/pages/classRoom/classRoom?test=1',
      // events: {
        
      //   acceptClassData: function (data) {
      //     console.log(data)
      //   }
      // },
      // success: function (res) {
      //   res.eventChannel.emit("acceptClassData", {
      //     data: classData
      //   })
      // }
    })
    console.log("班级详情", classData)

  },
  slideButtonTap(e) {
    let _this = this
    let classData = e.currentTarget.dataset.class
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/deleteClass' : 'https://www.wantstong.com/api/deleteClass',
      method: "POST",
      data: {
        classData: classData,
        user: _this.data.user
      },
      success: res => {
        wx.showToast({
          title: '成功',
        })

        let _this = this
        let openId = ""
        wx.getStorage({
          key: 'user',
          success: res => {
            console.log(res)
            openId = res.data['userId']
            wx.request({
              // url: 'http://localhost:88/api/searchClass',
              url: env != 1 ? 'http://localhost:88/api/searchClass' : 'https://www.wantstong.com/api/searchClass',
              method: 'POST',
              data: {
                openId: openId,
              },
              success: res => {
                console.log(res)
                _this.setData({
                  classData: res.data.res
                })
              }
            })
          }
        })

      }
    })

    console.log('slide button tap', e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      slideButtons: [{
        type: 'warn',
        text: '删除',
        extClass: 'test',
        src: '/page/weui/cell/icon_del.svg', // icon的路径
      }],
    });
    let app = getApp()
    let env = app.globalData.env
    let _this = this
    let openId = ""
    wx.getStorage({
      key: 'user',
      success: res => {
        console.log(res)
        _this.setData({
          user: res.data
        })
        openId = res.data['userId']
        wx.request({
          // url: 'http://localhost:88/api/searchClass',
          url: env != 1 ? 'http://localhost:88/api/searchClass' : 'https://www.wantstong.com/api/searchClass',
          method: 'POST',
          data: {
            openId: openId,
          },
          success: res => {
            console.log(res)
            _this.setData({
              classData: res.data.res
            })
          }
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let app = getApp()
    let env = app.globalData.env
    let _this = this
    let openId = ""
    wx.getStorage({
      key: 'user',
      success: res => {
        console.log(res)
        openId = res.data['userId']
        wx.request({
          // url: 'http://localhost:88/api/searchClass',
          url: env != 1 ? 'http://localhost:88/api/searchClass' : 'https://www.wantstong.com/api/searchClass',
          method: 'POST',
          data: {
            openId: openId,
          },
          success: res => {
            console.log(res)
            _this.setData({
              classData: res.data.res
            })
          }
        })
      }
    })



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let app = getApp()
    let env = app.globalData.env
    let _this = this
    let openId = ""
    wx.getStorage({
      key: 'user',
      success: res => {
        console.log(res)
        openId = res.data['userId']
        wx.request({
          // url: 'http://localhost:88/api/searchClass',
          url: env != 1 ? 'http://localhost:88/api/searchClass' : 'https://www.wantstong.com/api/searchClass',
          method: 'POST',
          data: {
            openId: openId,
          },
          success: res => {
            console.log(res)
            _this.setData({
              classData: res.data.res
            })
          }
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})